package com.ews.onlineexchange.rest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;

import com.ews.onlineexchange.controller.BpmnTaskExporter;

@Path( value = "/bpmn" )
public class BpmnTaskRest {
	
	
	@Inject
	BpmnTaskExporter bpmnTaskExporter;
	@GET    
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
	@Path( value = "/todoTaskExport" )
	public Response exportTodoTaskList(@QueryParam("dict") String dict,@QueryParam("processKey") String processKey,@QueryParam("dictId") String dictId,@QueryParam("dictTitle") String dictTitle) throws IOException {
		String fileName = "评价事项.xlsx";
		if(StringUtils.isNotBlank(dictId))fileName = dictTitle+".xlsx";
		Workbook workbook = bpmnTaskExporter.export(processKey,dictId);
		File tempFile = File.createTempFile("ONLINEEXCHANGE", "TEMPFILE");
		if(!tempFile.exists()) {
			tempFile.createNewFile();
		}
		FileOutputStream fileOut = new FileOutputStream(tempFile);
		workbook.write(fileOut);		
		workbook.close();
		ResponseBuilder response = Response.ok( tempFile );
		response.header("Content-Disposition", "attachment;filename="+new String(fileName.getBytes(),"ISO8859-1"));
		return response.build();
	}

}
